package com.ycy.temperature.view.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AutoCompleteTextView;
import android.widget.TextView;

import com.jude.utils.JUtils;
import com.qmuiteam.qmui.util.QMUIStatusBarHelper;
import com.ycy.temperature.R;
import com.ycy.temperature.bean.LoginBean;
import com.ycy.temperature.date.net.UserNetWorks;
import com.ycy.temperature.date.utils.CheckUtils;
import com.ycy.temperature.date.utils.SharedPreferencesUtils;
import com.ycy.temperature.view.base.BaseActivity;

import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import rx.Subscriber;

/**
 * Created by MI on 2018/7/17.
 */

public class LoginActivity extends BaseActivity {
    @BindView(R.id.Text_Mobile)
    AutoCompleteTextView TextMobile;
    @BindView(R.id.Text_Password)
    AutoCompleteTextView TextPassword;
    @BindView(R.id.Text_Login)
    TextView TextLogin;
    @BindView(R.id.Text_Register)
    TextView TextRegister;
    @BindView(R.id.Text_Forget)
    TextView TextForget;

    public static boolean isChinaPhoneLegal(String str) throws PatternSyntaxException {
        String regExp = "^((13[0-9])|(15[^4])|(18[0,2,3,5-9])|(17[0-8])|(147))\\d{8}$";
        Pattern p = Pattern.compile(regExp);
        Matcher m = p.matcher(str);
        return m.matches();
    }

    @Override
    protected int getLayout() {
        return R.layout.activity_login;
    }

    @Override
    protected void setData() {
        TextLogin.setClickable(false);
        TextLogin.setEnabled(false);
        TextLogin.setBackgroundResource(R.color.bg_LightGray);
    }

    @Override
    protected void init(Bundle inBundle) {
        ButterKnife.bind(this);
        QMUIStatusBarHelper.setStatusBarLightMode(this);
        TextMobile.addTextChangedListener(new TextWatcher() {

            @Override
            public void beforeTextChanged(final CharSequence charSequence, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence charSequence, int start, int before, int count) {
                if (charSequence.length() < 11) {
                    TextLogin.setClickable(false);
                    TextLogin.setEnabled(false);
                    TextLogin.setBackgroundResource(R.color.bg_LightGray);
                } else {
                    if (!isChinaPhoneLegal(TextMobile.getText().toString())) {
                        TextLogin.setClickable(false);
                        TextLogin.setEnabled(false);
                        TextLogin.setBackgroundResource(R.color.bg_LightGray);
                    } else {
                        TextLogin.setClickable(true);
                        TextLogin.setEnabled(true);
                        TextLogin.setBackgroundResource(R.color.bg_Blue);
                    }
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    @OnClick({R.id.Text_Login, R.id.Text_Register, R.id.Text_Forget})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.Text_Login:
                if (!getVerification()) return;
                getLogin();
                break;
            case R.id.Text_Register:
                startActivity(new Intent(LoginActivity.this, RegisterActivity.class));
                break;
            case R.id.Text_Forget:
                startActivity(new Intent(LoginActivity.this, ForgetActivity.class));
                break;
        }
    }

    public boolean getVerification() {
        if (CheckUtils.isEmpty(TextMobile.getText().toString())) {
            JUtils.Toast("请输入手机号");
            return false;
        }
        if (CheckUtils.isEmpty(TextPassword.getText().toString())) {
            JUtils.Toast("请输入密码");
            return false;
        }
//        if (!isChinaPhoneLegal(TextMobile.getText().toString())) {
//            JUtils.Toast("请输入正确手机号");
//            return false;
//        }
        return true;
    }

    public void getLogin() {
        UserNetWorks.getLogin(TextMobile.getText().toString(), TextPassword.getText().toString(), new Subscriber<LoginBean>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {
                e.printStackTrace();
                JUtils.Toast("网络错误");
            }

            @Override
            public void onNext(LoginBean loginBean) {
                if (loginBean.getResponseState().equals("1")) {
                    JUtils.Toast(loginBean.getMsg());
                    SharedPreferencesUtils.putBoolean("login", "boolean", true);
                    SharedPreferencesUtils.putString("token", "String", loginBean.getData().getToken());
                    startActivity(new Intent(LoginActivity.this, MainActivity.class));
                    finish();
                } else {
                    JUtils.Toast(loginBean.getMsg());
                }
            }
        });
    }
}
